package org.jeecg.modules.zhoupackage.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.zhoupackage.mapper.*;
import org.jeecg.modules.zhoupackage.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;

/**
 * 团订工具类
 *
 * @author zxl
 * @since 20201008
 */
@Component
public class TeamOrderUtil {

    @Autowired
    MyTeamMudoleMapper teamMudoleMapper;

    @Autowired
    MyCustomerDepartMapper customerDepartMapper;

    @Autowired
    MyDepartProductMapper departProductMapper;

    @Autowired
    MyMlccProductInfoMapper productInfoMapper;

    @Autowired
    MyCustomeInfoMapper customeInfoMapper;

    /**
     * 编辑团订模板(查询详情)
     *
     * @param moduleSn
     * @return
     */
    public MlccTeamModuleInfo updateModule(String moduleSn) {
        /*根据模板的编号查到这条模板数据*/
        QueryWrapper<MlccTeamModuleInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn", moduleSn);
        MlccTeamModuleInfo teamModuleInfo = teamMudoleMapper.selectOne(wrapper);
        /*模板数据中有一个字段是客户ID，包含了所有需要查询的数据*/
        String customerId = teamModuleInfo.getCustomerId();
        /*根据客户ID，可以在客户部门关系表中找到对应的部门*/
        QueryWrapper<MlccCustomerDepart> customerDepartQueryWrapper = new QueryWrapper<>();
        customerDepartQueryWrapper.eq("customer_id", customerId);
        MlccCustomerDepart depart = customerDepartMapper.selectOne(customerDepartQueryWrapper);
        /*得到了部门数据，就可以得到部门下的人员列表与部门下的产品列表*/
        /*首先得到部门下的产品列表*/
        HashSet<String> producetids = new HashSet<>();
        QueryWrapper<MlccDepartProduct> wrapper1 = new QueryWrapper<>();
        wrapper.eq("customer_depart_id", depart.getId());
        List<MlccDepartProduct> departProductList = departProductMapper.selectList(wrapper1);
        for (MlccDepartProduct departProduct : departProductList) {
            /*现在拿到了所有的产品ID*/
            producetids.add(departProduct.getProductId());
        }
        /*查询具体的产品列表，需要包含两个条件*/
        QueryWrapper<MlccProductInfo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("order_sn", moduleSn).in("id", producetids);
        teamModuleInfo.setProductList(productInfoMapper.selectList(wrapper2));

        // 接下来查询部门下的人员列表(花名册)
        QueryWrapper<MlccCustomerInfo> wrapper3 = new QueryWrapper<>();
        wrapper3.eq("depart_id", depart.getId()).eq("is_company", 0);
        teamModuleInfo.setCustomerInfoList(customeInfoMapper.selectList(wrapper3));

        // 获取人员的量体数据列表

        return teamModuleInfo;
    }
}
